#include "DHT.h"
#include "Wire.h"
#include "LiquidCrystal.h"

// Pin definitions
// Output pins
const int HEATER_PIN = 8;
const int COOLER_PIN = 10;
const int FAN_PIN = 9;

// input controls
const int DHT_PIN = 2;  // Temperature and humidity sensor
const int FAN_OVERRIDE_PIN = 3;
// const int ROTARY_KNOB_PINS =;
// const int ROTARY_KNOB_BUTTON =;
// const int HEATER_ON_PIN =;
// const int COOLER_ON_PIN =;

// SoftwareSerial mySerial(10, 11); // RX, TX

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11
const int DHTTYPE = DHT22;   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHT_PIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

DHT dht(DHT_PIN, DHTTYPE);
// Connect via i2c, default address #0 (A0-A2 not jumpered)
LiquidCrystal lcd(0);

#define F2C(x) (((x - 32) * 5) / 9)

long lastReadTime = 0;

float absoluteMinHeatDegreesC = F2C(55);

float minHeatDegreesC = F2C(72); // 21;
float maxCoolDegreesC = F2C(75); // 23.888889;

const float heatHistDegreesC = 1;
const float coolHistDegreesC = 1;

boolean coolOn = false;
boolean heatOn = false;

long heatOnTimeSecs = 0;
long coolOnTimeSecs = 0;

boolean heatEnabled = true;
boolean coolEnabled = true;

float humidity = NAN;
float degreesCelcius = NAN;

const int minOnTimeSecs = 60;
const int minOffTimeSecs = 3 * 60;

void setup() {
  Serial.begin(19200);
 
  //  while (!Serial) {
  //    ; // wait for serial port to connect. Needed for Leonardo only
  // }

  dht.begin();
  
  // set up the LCD's number of rows and columns: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Ready");
  
  pinMode(HEATER_PIN, OUTPUT);
  pinMode(COOLER_PIN, OUTPUT);
  pinMode(FAN_PIN, OUTPUT);
}

void loop() {
  if (Serial.available()) {
    char c = Serial.read();
    switch (c) {
      case 'c':
        digitalWrite(COOLER_PIN, LOW);
        break;
      case 'C':
        digitalWrite(COOLER_PIN, HIGH);
        break;
      case 'h':
        digitalWrite(HEATER_PIN, LOW);
        break;
      case 'H':
        digitalWrite(HEATER_PIN, HIGH);
        break;
      case 'f':
        digitalWrite(FAN_PIN, LOW);
        break;
      case 'F':
        digitalWrite(FAN_PIN, HIGH);
        break;
    }
    // mySerial.write(Serial.read());
  }

  long now = millis();
  if (now < lastReadTime) {
    lastReadTime = 0;
  }
  
  if (now - lastReadTime > 2100) {
    readSensor();
    lastReadTime = now;
    checkTemp();
  }
}

void checkTemp() {
  if (isnan(degreesCelcius)) {
    return;
  }
  
  lcd.setCursor(0, 1);
  
  if (degreesCelcius < minHeatDegreesC && !heatOn) {
    lcd.print("turn on heat");
    heatOn = true;
    digitalWrite(HEATER_PIN, HIGH);
  } else if (degreesCelcius > minHeatDegreesC + heatHistDegreesC && heatOn) {
    lcd.print("turn off heat");
    heatOn = false;
    digitalWrite(HEATER_PIN, LOW);
  }
  
  if (degreesCelcius > maxCoolDegreesC && !coolOn) {
    lcd.print("turn on cool");
    coolOn = true;
    digitalWrite(COOLER_PIN, HIGH);
  } else if (degreesCelcius < maxCoolDegreesC - heatHistDegreesC && coolOn) {
    lcd.print("turn off cool");
    coolOn = false;
    digitalWrite(COOLER_PIN, LOW);
  }
}

void readSensor() {
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(t) || isnan(h)) {
    Serial.println("Failed to read from DHT");
  } else {
    humidity = h;
    degreesCelcius = t;
    float fahr = t * (9.0 / 5.0) + 32;
    Serial.print(humidity);
    Serial.print(",");
    Serial.println(degreesCelcius);
    
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(fahr);
    lcd.print((char)223);
    lcd.print("F  ");
    lcd.print(humidity);
    lcd.print("%");
  }
}

